package com.zoweunion.mechanic.service.base.impl;

import com.zoweunion.mechanic.dao.base.CarModelManagementDao;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.BaseService;
import com.zoweunion.mechanic.service.base.CarModelManagementService;
import com.zoweunion.mechanic.util.Orgin;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CarModelManagementServiceImpl extends BaseService implements CarModelManagementService {

    @Autowired
    private CarModelManagementDao carModelManagementDao;

    @Override
    public Map<String, Object> list(User currentUser, Map<String, Object> reqMap) {

        //1.设置分页的limit和offset
        Map<String, Object> pageMap = this.appPage(reqMap);
        int limit = Integer.valueOf(pageMap.get("limit").toString());
        int total = carModelManagementDao.listCount(reqMap);
        List<Map<String, Object>> list = carModelManagementDao.list(reqMap);
        int pages = getPages(total, limit);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", total);
        resultMap.put("pages", pages);
        resultMap.put("list", list);

        return resultMap;
    }

    @Override
    public boolean save(User currentUser, Map<String, Object> params) {
        this.setBasicParams(currentUser, params);
        if (params.get("id") != null && StringUtils.isNotBlank(params.get("id").toString())) {
            carModelManagementDao.insert(params);
        } else {
            carModelManagementDao.update(params);
        }
        return true;
    }

    @Override
    public Map<String, Object> detail(User currentUser, Map<String, Object> reqMap) {
        return carModelManagementDao.detail(reqMap);
    }

    @Override
    public boolean delete(User currentUser, Map<String, Object> params) {
        this.setBasicParams(currentUser, params);
        carModelManagementDao.delete(params);
        return true;
    }

    @Override
    public Map<String, Object> selectFuelTank(Map<String, Object> map) {
        String thingsId = Orgin.getString(map, "thingsId");
        Map<String, Object> hashMap = new HashMap<>();
        hashMap=carModelManagementDao.selectFuelTank(thingsId);
        if (hashMap==null){
            hashMap.put("fuel_tank",300);
        }
        return hashMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFuelTank(Map<String, Object> map) {
        String id = Orgin.getString(map, "id");
        if (!id.isEmpty()&&!StringUtils.isBlank(id)){
            carModelManagementDao.updateFuelTank(map);
        }else {
            Map<String, Object> objectMap = carModelManagementDao.selectInfo(map);
            objectMap.put("fuel_tank",map.get("fuel_tank"));
            carModelManagementDao.insertFuelTank(objectMap);
        }
    }

}
